public class PreInOrderCreateTree extends BinaryTree{
    public int preIndex;
    //创建函数，传递前序遍历和中序遍历的数组
    public TreeNode buildTree(int[] preorder, int[] inorder) {
       return bulidTreeChild(preorder,inorder,0,inorder.length-1);
    }
    public TreeNode bulidTreeChild(int[] preorder, int[] inorder,int inBegin, int inEnd) {
        if(inBegin > inEnd) {
            return null;
        }
        //创建根节点
        TreeNode root = new TreeNode(preorder[preIndex]);
        //在中序遍历的字符串当中找到根节点
        int rootIndex = findRootIndex(inorder,inBegin,inEnd,preorder[preIndex]);
        preIndex++;//F
        //递归创建左树和右树
        root.left = bulidTreeChild(preorder,inorder,inBegin,rootIndex-1);
        root.right = bulidTreeChild(preorder,inorder,rootIndex+1,inEnd);
        return root;
    }
    private int findRootIndex(int[] inorder,int inBegin,int inEnd,int key) {
        for (int i = inBegin;i<=inEnd;i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
}